package com.blqy.fr.log.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.blqy.fr.common.annotation.BlQyLogin;
import com.blqy.fr.common.result.Result;
import com.blqy.fr.common.util.AuthContextHolder;
import com.blqy.fr.log.service.LogService;
import com.blqy.fr.model.log.Log;
import com.blqy.fr.user.client.TeamInfoFeignClient;
import com.blqy.fr.user.client.TemplateInfoFeignClient;
import com.blqy.fr.user.client.UserInfoFeignClient;
import com.blqy.fr.vo.Log.logContentVo;
import com.blqy.fr.vo.Log.logCoverVo;
import com.blqy.fr.vo.template.TemplateResponseVo;
import com.blqy.fr.vo.user.UserDetailVo;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/user/log")
public class LogController {
    @Autowired
    private LogService logService;

    @Resource
    private UserInfoFeignClient userInfoFeignClient;

    @Resource
    private TeamInfoFeignClient  teamInfoFeignClient;


    @Resource
    private TemplateInfoFeignClient templateInfoFeignClient;


    /**
     * 获取当前团队下的模板标签
     */
    @BlQyLogin
    @GetMapping("/getTemplateTag")
    public Result<TemplateResponseVo> getTemplateTag() {
        Long teamId = teamInfoFeignClient.getTeamIdByUserId(AuthContextHolder.getUserId()).getData();
        Assert.notNull(teamId, "请先加入团队");
        Result<Long> templateIdByTeamId = templateInfoFeignClient.getTemplateIdByTeamId(teamId);
        Long templatetId = templateIdByTeamId.getData();
        Assert.notNull(templatetId, "请先创建模板");

        Result<String> templateNameByTeamId = templateInfoFeignClient.getTemplateNameByTeamId(teamId);
        String templateName = templateNameByTeamId.getData();
        Assert.notNull(templateName, "没有该模板");

        Result<TemplateResponseVo> templateResponseVoResult = templateInfoFeignClient.getTemplateInfo(templateName);
        return templateResponseVoResult;
    }

    /**
     * 添加日志
     *
     * @param log
     * @return
     */

    @BlQyLogin
    @PostMapping("/addLog")
    public Result addLog(@RequestBody logContentVo log){
        Log log1 = new Log();

        Long teamId = teamInfoFeignClient.getTeamIdByUserId(AuthContextHolder.getUserId()).getData();
        Assert.notNull(teamId, "请先加入团队");
        Result<Long> templateIdByTeamId = templateInfoFeignClient.getTemplateIdByTeamId(teamId);
        Long templatetId = templateIdByTeamId.getData();
        Assert.notNull(templatetId, "请先创建模板");
        log1.setUserId(AuthContextHolder.getUserId());
        log1.setTemplateId(templatetId);
        log1.setTeamId(teamId);
        log1.setLogContent(log.getLogContent());
        log1.setSubmit(log.getSubmit());
        log1.setStat(log.getStatus());

        logService.save(log1);
        return Result.ok();
    }


    /**
     * 获取当前团队id下所有日志
     *
     * @return
     */
    @BlQyLogin
    @GetMapping("/getLogCover")
    public Result<List<logCoverVo>> getLogCover() {
        Long userId = AuthContextHolder.getUserId();

        List<logCoverVo> logs = new ArrayList<>();
        //  远程调用获取团队id
        Long teamId = teamInfoFeignClient.getTeamIdByUserId(userId).getData();
        Assert.notNull(teamId, "请先加入团队");

        List<Log> log = logService.list(new LambdaQueryWrapper<Log>()
                .eq(Log::getTeamId, teamId)
                .orderByDesc(Log::getCreateTime)
        );

        log.forEach(log1 -> {
            logCoverVo logCoverVo = new logCoverVo();

            Long userId1 = log1.getUserId();
            logCoverVo.setUserId(userId1);

            Long id = log1.getId();
            logCoverVo.setLogId(id);

            Integer submit = log1.getSubmit();
            logCoverVo.setSubmit(submit);

            Integer stat = log1.getStat();
            logCoverVo.setStatus(stat);

            Map<String, Object> logContent = log1.getLogContent();
            logCoverVo.setLogContent(logContent);

            Date createTime = log1.getCreateTime();
            logCoverVo.setCreateTime(createTime);

            //  远程调用获取用户名称和头像
            Result<UserDetailVo> userThree = userInfoFeignClient.findUserThree(userId);
            UserDetailVo data = userThree.getData();
            String avatar = data.getAvatar();
            logCoverVo.setAvatar(avatar);
            String nickName = data.getNickName();
            logCoverVo.setNickName(nickName);

            //  远程调用获取评阅数
            logCoverVo.setNumberOfComments(0);
            logs.add(logCoverVo);
        });
        return Result.ok(logs);
    }



    /**
     * 获取日志详情
     *
     * @param logid
     * @return
     */
    @BlQyLogin
    @GetMapping("/getLog/{logid}")
    public Result<logContentVo> getLog(@PathVariable Long logid) {
        logContentVo logContentVo = new logContentVo();
        Log log = logService.getById(logid);
        Assert.notNull(log, "日志不存在");
        Map<String, Object> logContent = log.getLogContent();
        Integer stat = log.getStat();
        logContentVo.setStatus(stat);
        Integer submit = log.getSubmit();
        logContentVo.setSubmit(submit);
        logContentVo.setLogContent(logContent);
        return Result.ok(logContentVo);
    }



    @DeleteMapping("/DeleteLog/{logid}")
    public Result DeleteLog(@PathVariable Long logid){
        logService.removeById(logid);
        return Result.ok();
    }
}
